import type {
  AddReq,
  DelReq,
  EditReq,
  InfoReq,
  UserPageRes,
} from '@fast-crud/fast-crud';

import type { AxiosRequestConfig } from '@vben/request';

import type { DefTenantApplicationRelModel } from './model/defTenantApplicationRelModel';

import type { PageParams, PageResult } from '#/api';
import type { TreeItem } from '#/components/tree';

import { requestClient } from '#/api/request';

const MODULAR = 'defTenantApplicationRel';

// tips: 建议在ServicePrefixEnum中新增：SYSTEM = '/system'，并将下方代码改为： const ServicePrefix = ServicePrefixEnum.SYSTEM;
// tips: /system 需要与 lamp-gateway-server.yml中配置的Path一致，否则无法正常调用接口！！！
// const ServicePrefix = ServicePrefixEnum.SYSTEM;
const ServicePrefix = '/system';

export const DefTenantApplicationRelConfig = {
  Save: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'POST',
  } as AxiosRequestConfig,
  Update: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'PUT',
  } as AxiosRequestConfig,
};

export namespace DefTenantApplicationRelApi {
  /**
   * 根据条件查询分页数据
   */
  export async function page(
    params: PageParams<DefTenantApplicationRelModel.DefTenantApplicationRelPageQuery>,
  ) {
    return requestClient.post<
      PageResult<DefTenantApplicationRelModel.DefTenantApplicationRelResultVO>
    >(`${ServicePrefix}/${MODULAR}/page`, params);
  }
  /**
   * 根据id查询单条数据
   */
  export async function get(id: string) {
    return requestClient.get<DefTenantApplicationRelModel.DefTenantApplicationRelResultVO>(
      `${ServicePrefix}/${MODULAR}/${id}`,
    );
  }
  export async function detail(id: string) {
    return requestClient.get<DefTenantApplicationRelModel.DefTenantApplicationRelResultVO>(
      `${ServicePrefix}/${MODULAR}/detail`,
      { params: { id } },
    );
  }

  /** 首次授权（覆盖授权） */
  export async function grant(
    params: DefTenantApplicationRelModel.DefTenantApplicationRelSaveVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/grant`,
      params,
    );
  }

  /** 重新授权 */
  export async function renewal(
    params: DefTenantApplicationRelModel.DefTenantApplicationRelUpdateVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/renewal`,
      params,
    );
  }

  /**
   * 取消授权
   * @param params id
   */
  export async function cancel(params: string[]) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/cancel`,
      params,
    );
  }

  /**
   * 保存
   */
  export async function save(
    params: DefTenantApplicationRelModel.DefTenantApplicationRelSaveVO,
  ) {
    return requestClient.post<DefTenantApplicationRelModel.DefTenantApplicationRelResultVO>(
      DefTenantApplicationRelConfig.Save.url as string,
      params,
    );
  }

  /**
   * 修改
   */
  export async function update(
    params: DefTenantApplicationRelModel.DefTenantApplicationRelUpdateVO,
  ) {
    return requestClient.put<DefTenantApplicationRelModel.DefTenantApplicationRelResultVO>(
      DefTenantApplicationRelConfig.Update.url as string,
      params,
    );
  }

  /**
   * 删除
   */
  export async function remove(data: string[]) {
    return requestClient.delete<boolean>(`${ServicePrefix}/${MODULAR}`, {
      data,
    });
  }

  // fast-crud 需要的接口
  export const pageRequest = async (
    pageQuery: PageParams<DefTenantApplicationRelModel.DefTenantApplicationRelPageQuery>,
  ): Promise<UserPageRes> => {
    return await page(pageQuery);
  };

  export const infoRequest = async (ctx: InfoReq): Promise<UserPageRes> => {
    const { row } = ctx;

    const record = await detail(row.id);
    row.resourceList = record.resourceList as TreeItem[];
    row.checkedList = record.checkedList as string[];
    row.resourceIdList = record.checkedList as string[];

    // 请求后台查询数据
    return row;
  };

  export const addRequest = async (req: AddReq) => {
    const { form } = req;
    return await save(form);
  };

  export const editRequest = async (ctx: EditReq) => {
    const { form } = ctx;
    return await renewal(form);
  };

  export const delRequest = async (ctx: DelReq) => {
    const { row } = ctx;
    return await remove([row.id]);
  };
}
